package com.zbycorp.generatecode.dto;

import com.intellij.ide.fileTemplates.impl.UrlUtil;
import com.intellij.util.ExceptionUtil;
import com.zbycorp.generatecode.entity.Template;
import com.zbycorp.generatecode.entity.TypeMapper;
import com.zbycorp.generatecode.utils.CollectionUtil;
import com.zbycorp.generatecode.utils.JSON;
import com.zbycorp.generatecode.utils.StringUtil;
import lombok.Data;

import java.util.*;

/**
 * @author YCL
 * @date 2023/3/24
 */
@Data
public class SettingsStorageDTO {

    public static SettingsStorageDTO defaultVal() {
        try {
            String json = UrlUtil.loadText(Objects.requireNonNull(SettingsStorageDTO.class.getResource("/defaultConfig.json")));
            return JSON.parse(json, SettingsStorageDTO.class);
        } catch (Exception e) {
            ExceptionUtil.rethrow(e);
        }

        // 配置文件加载失败，直接创建配置
        SettingsStorageDTO storage = new SettingsStorageDTO();
        storage.typeMapperList = new ArrayList<>();
        storage.templateMap = new HashMap<>(5);
        return storage;
    }

    public void fillDefaultVal() {
        SettingsStorageDTO defaultVal = defaultVal();
        if (CollectionUtil.isEmpty(this.typeMapperList)) {
            this.typeMapperList = defaultVal.getTypeMapperList();
        }
        if (CollectionUtil.isEmpty(this.templateMap)) {
            this.templateMap = defaultVal.getTemplateMap();
        }
        if (StringUtil.isEmpty(this.definedPackage)) {
            this.definedPackage = defaultVal.getDefinedPackage();
        }
        if (StringUtil.isEmpty(this.domainPackage)) {
            this.domainPackage = defaultVal.getDomainPackage();
        }
    }

    private String author;
    private String definedPackage;
    private String domainPackage;
    private String tablePrefix;

    private List<TypeMapper> typeMapperList = new ArrayList<>();

    private Map<String, List<Template>> templateMap = new HashMap<>(5);

}
